package com.chushouya.product.dao.repository;

import com.general.framework.core.lang.Strings;
import com.general.framework.core.schema.PageResult;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.product.dao.entity.TempEvaluateItemEntity;
import com.chushouya.product.dao.mapper.TempEvaluateItemEntityMapper;
import com.chushouya.product.dto.admin.tempevaluate.TempEvaluateItemQuery;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

@Repository
public class TempEvaluateItemRepository extends BaseRepository<TempEvaluateItemEntityMapper, TempEvaluateItemEntity, Long> {
    
    public List<TempEvaluateItemEntity> selectList(TempEvaluateItemQuery query) {
        Example example = buildExample(query);
        return this.selectByExample(example);
    }

    public PageResult<TempEvaluateItemEntity> selectPageResult(TempEvaluateItemQuery query) {
        Example example = this.buildExample(query);
        return this.selectPage(query.getPageRequest(), example);
    }
    
    public TempEvaluateItemEntity selectOne(TempEvaluateItemQuery query) {
        Example example = buildExample(query);
        return this.selectFirstOneByExample(example);
    }

    public int deleteByTempId(Long tempId) {
        if (Objects.isNull(tempId)) {
            return 0;
        }
        Example example = new Example(TempEvaluateItemEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("tempId", tempId);
        return this.deleteByExample(example);
    }

    private Example buildExample(TempEvaluateItemQuery query) {
        Example example = new Example(TempEvaluateItemEntity.class);
        Example.Criteria criteria = example.createCriteria();
        if (Objects.nonNull(query.getTempItemId())) {
            criteria.andEqualTo("tempItemId", query.getTempItemId());
        }
        if (Objects.nonNull(query.getTempId())) {
            criteria.andEqualTo("tempId", query.getTempId());
        }
        if (Objects.nonNull(query.getItemId())) {
            criteria.andEqualTo("itemId", query.getItemId());
        }
        if (Strings.isNotEmpty(query.getItemName())) {
            criteria.andLike("itemName", "%" + query.getItemName() + "%");
        }
        if (Objects.nonNull(query.getSelectType())) {
            criteria.andEqualTo("selectType", query.getSelectType());
        }
        example.orderBy("sort").asc();
        return example;
    }
}
